1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
22
23 import com.google.common.annotations.GwtCompatible;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import com.google.common.collect.testing.features.ListFeature;
27
28
29
30
31
32
33
34
35 @GwtCompatible(emulated = true)
36 public class ListSetTester<E> extends AbstractListTester<E> {
37 @ListFeature.Require(SUPPORTS_SET)
38 @CollectionSize.Require(absent = ZERO)
39 public void testSet() {
40 doTestSet(samples.e3);
41 }
42
43 @CollectionSize.Require(absent = ZERO)
44 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
45 @ListFeature.Require(SUPPORTS_SET)
46 public void testSet_null() {
47 doTestSet(null);
48 }
49
50 @CollectionSize.Require(absent = ZERO)
51 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
52 @ListFeature.Require(SUPPORTS_SET)
53 public void testSet_replacingNull() {
54 E[] elements = createSamplesArray();
55 int i = aValidIndex();
56 elements[i] = null;
57 collection = getSubjectGenerator().create(elements);
58
59 doTestSet(samples.e3);
60 }
61
62 private void doTestSet(E newValue) {
63 int index = aValidIndex();
64 E initialValue = getList().get(index);
65 assertEquals("set(i, x) should return the old element at position i.",
66 initialValue, getList().set(index, newValue));
67 assertEquals("After set(i, x), get(i) should return x",
68 newValue, getList().get(index));
69 assertEquals("set() should not change the size of a list.",
70 getNumElements(), getList().size());
71 }
72
73 @ListFeature.Require(SUPPORTS_SET)
74 public void testSet_indexTooLow() {
75 try {
76 getList().set(-1, samples.e3);
77 fail("set(-1) should throw IndexOutOfBoundsException");
78 } catch (IndexOutOfBoundsException expected) {
79 }
80 expectUnchanged();
81 }
82
83 @ListFeature.Require(SUPPORTS_SET)
84 public void testSet_indexTooHigh() {
85 int index = getNumElements();
86 try {
87 getList().set(index, samples.e3);
88 fail("set(size) should throw IndexOutOfBoundsException");
89 } catch (IndexOutOfBoundsException expected) {
90 }
91 expectUnchanged();
92 }
93
94 @CollectionSize.Require(absent = ZERO)
95 @ListFeature.Require(absent = SUPPORTS_SET)
96 public void testSet_unsupported() {
97 try {
98 getList().set(aValidIndex(), samples.e3);
99 fail("set() should throw UnsupportedOperationException");
100 } catch (UnsupportedOperationException expected) {
101 }
102 expectUnchanged();
103 }
104
105 @CollectionSize.Require(ZERO)
106 @ListFeature.Require(absent = SUPPORTS_SET)
107 public void testSet_unsupportedByEmptyList() {
108 try {
109 getList().set(0, samples.e3);
110 fail("set() should throw UnsupportedOperationException "
111 + "or IndexOutOfBoundsException");
112 } catch (UnsupportedOperationException tolerated) {
113 } catch (IndexOutOfBoundsException tolerated) {
114 }
115 expectUnchanged();
116 }
117
118 @CollectionSize.Require(absent = ZERO)
119 @ListFeature.Require(SUPPORTS_SET)
120 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
121 public void testSet_nullUnsupported() {
122 try {
123 getList().set(aValidIndex(), null);
124 fail("set(null) should throw NullPointerException");
125 } catch (NullPointerException expected) {
126 }
127 expectUnchanged();
128 }
129
130 private int aValidIndex() {
131 return getList().size() / 2;
132 }
133 }
134